//
//  AnswerSetView.m
//  JW_BasePorject
//
//  Created by JW on 2018/7/4.
//  Copyright © 2018年 JW. All rights reserved.
//

#import "AnswerSetView.h"

@implementation AnswerSetView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setupBasicView];
        
    }
    return self;
}


- (void)setupBasicView{
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH-100, 30)];
    titleLabel.attributedText = [self titleAttributedStr];
    [self addSubview:titleLabel];
    
    self.setSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-60, 10, 50, 30)];
    self.setSwitch.onTintColor = ZhuTiColor;
    NSUserDefaults *user =  [NSUserDefaults standardUserDefaults];
    BOOL isAllow = [[user objectForKey:@"allowsCellularAccess"] boolValue];
    self.setSwitch.on = isAllow;
    
    [self.setSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:self.setSwitch];
    
}


- (void)switchAction:(UISwitch *)sw{
    //0 关  1开
    NSUserDefaults *user =  [NSUserDefaults standardUserDefaults];
    BOOL isAllow = [[user objectForKey:@"allowsCellularAccess"] boolValue];
    if (sw.isOn != isAllow) {
        [user setObject:[NSNumber numberWithBool:sw.isOn] forKey:@"allowsCellularAccess"];
        [user synchronize];
        
    }
}

- (NSAttributedString *)titleAttributedStr{
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:@"单选速答 " attributes:@{ NSFontAttributeName: [UIFont systemFontOfSize:18],NSForegroundColorAttributeName: [UIColor blackColor]}];
    
    NSString *totalNum = @"选择后立即出答案";
    NSMutableAttributedString *totalStr = [[NSMutableAttributedString alloc] initWithString:totalNum attributes:@{ NSFontAttributeName: [UIFont systemFontOfSize:15],NSForegroundColorAttributeName: [UIColor lightGrayColor]}];
    [attributedStr appendAttributedString: totalStr];
    
    return attributedStr;
}

@end
